package sopt.dev.view;

import java.io.IOException;
import java.io.InputStream;

import sopt.dev.R;
import sopt.dev.data.CityAdatper;
import sopt.dev.data.CityVO;
import sopt.dev.data.ItemVO;
import sopt.dev.util.GagebuApp;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CityItem extends LinearLayout implements OnClickListener {

	private Context mContext;
	private ImageView ivCity;
	private TextView tvCityName;
	private TextView tvInDate;
	private TextView tvOutDate;
	private CityVO<ItemVO> mCity;
	CityAdatper adapter;

	public interface OnCityClickListener {
		public void onCityClick(CityVO<ItemVO> city);
	}

	OnCityClickListener mListener;

	public void setOnCityClickListener(OnCityClickListener listener) {
		this.mListener = listener;
	}

	public CityItem(Context context) {
		super(context);
		this.mContext = GagebuApp.getContext();
		LayoutInflater.from(mContext).inflate(R.layout.city_row, this);
		ivCity = (ImageView) findViewById(R.id.city_row_cityview);
		tvCityName = (TextView) findViewById(R.id.city_row_cityname);
		tvInDate = (TextView) findViewById(R.id.city_row_indate);
		tvOutDate = (TextView) findViewById(R.id.city_row_outdate);
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.city_row_cityview: {
			if (mListener != null)
				mListener.onCityClick(mCity);
		}
		}

	}

	public void setData(CityVO<ItemVO> data) {
		mCity = data;
		tvCityName.setText(data.getCityName());
		tvInDate.setText(data.getInString() + " ~ ");
		tvOutDate.setText(data.getOutString());

		// get input stream
		InputStream ims = null;
		try {
			ims = mContext.getAssets().open(data.getPicPath());
		} catch (IOException e) {
			e.printStackTrace();
		}
		// load image as Drawable
		Drawable d = Drawable.createFromStream(ims, null);
		// set image to ImageView
		ivCity.setImageDrawable(d);

	}

}
